<?php

class Controller_News extends Controller_Base
{
	private $show_in_page = 7;
	
	public function __construct($parts) {		
		$this->parts        = $parts;
		$this->common_vars  = Registry::get("common_vars");
		$this->table_module = PRFX."news";
	}
	
    public function out()
	{     
		if(empty($this->parts[3]) && (!empty($this->parts[2]) && ($this->parts[2] != "page"))) {
			$SQL_DOP = " AND `Sys_name`= '".$this->parts[2]."' ";
			$out['is_info_view_news'] = '1';			
		} else {			
			Registry::set("title", functions::createTitle(array($this->common_vars["structure_name"]), ''));
		}
		$_PAGING = new navygation_page(Registry::get("MDB"), $this->parts[1], $this->parts[3]);
		$r = & $_PAGING->get_page("SELECT * FROM `".$this->table_module."` WHERE `Show`='1' AND `Parent`='".$this->common_vars["structure_id"]."' ".$SQL_DOP." ORDER BY `Date` DESC ");
		if (PEAR::isError($r)) {
			die($r->getMessage().' ::: '.$r->getUserInfo());
		}				
		if($r->numRows())
		{					
			$i = 0;
			$C_ARRAY = array();				
			while($p = $r->fetchRow(MDB2_FETCHMODE_ASSOC))
			{				
				/*
				 * Если открыта новость 
				 */
				if(!empty($SQL_DOP))
				{
					for($j=1;$j<=40;$j++)
					{
						unset($tmp_image, $width, $height);						
						$tmp_image = functions::getImage("news", $p["id"], $p["name"], $j, "450x213_news_gallery_trumbnail.jpg", "background", "md5", &$width, &$height);						
						if($tmp_image) {
							$C_ARRAY[$i]["image_gallery"][$j]["pic"] = $tmp_image;
							$C_ARRAY[$i]["image_gallery"][$j]["w"] = $width;
							$C_ARRAY[$i]["image_gallery"][$j]["h"] = $height;
						}
					}
					$C_ARRAY[$i]["btext"] = $p["btext"];
					/* Вернутся */
					//if(substr_count($_SERVER['HTTP_REFERER'], $this->parts[1])) {
						$C_ARRAY[$i]["back_link"] = "/".$this->parts[1].ENDURL;
					//}
					
					/* Add Array for Title */
					Registry::set("title", functions::createTitle(array($this->common_vars["structure_name"], $p["name"]), $p["title"]));
					/* SEO */
					Registry::set("description", $p["description"]);
					Registry::set("keywords",    $p["keywords"]);					
				}
				/*
				 * Если список новостей 
				 */
				else
				{
					$C_ARRAY[$i]["image"] = functions::getImage("news", $p["id"], $p["name"], 1, "_106x75_news_trumbnail.jpg", "normal", "md5");
					$C_ARRAY[$i]["stext"]    = nl2br($p["stext"]);
					$C_ARRAY[$i]["sys_name"] = $this->parts[1]."/".$p["sys_name"];
				}
				$C_ARRAY[$i]["date"] = strtotime($p["date"]);
				$C_ARRAY[$i]["name"] = $p["name"];
				$i++;
			}
			
			$out['navygation']['prev'] = $_PAGING->get_prev_page_link();
			$out['navygation']['next'] = $_PAGING->get_next_page_link();
			$out['navygation']['pages'] = $_PAGING->get_page_links();
			$out['navygation']['result_text'] = $_PAGING->get_result_text();
			
			$out['c_array']    = $C_ARRAY;								
		}
		else {
			$e = new Error404;
		}     
		
		$out['header']      = $this->common_vars["structure_name"];
		$out['header_link'] = $this->parts[1];
		
		/* Меню новостей справа */
		//$out['menu_news'] = menu::getSubMenuNews(56);
		/* Меню акции справа */
		//$out['menu_action'] = menu::getSubMenuNews(57);
	
		// print_r($out);		
		Registry::get("smarty")->assign($out);
		return Registry::get("smarty")->fetch(TEMPLATE_SITE.$this->common_vars["controller_template"], Registry::get("cache_id"));	
    }
}

$controller = new Controller_News($parts);
$site_module_out = $controller->out();

?>